package net.huashitong.scheduling.service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.utils.ObjectUtils;

import net.huashitong.scheduling.dao.FinanceIncomeDao;
import net.huashitong.scheduling.entity.FinanceIncome;

@Service
public class FinanceIncomeService extends StringPKService<FinanceIncome> {
    @Autowired
    private FinanceIncomeDao dao;
    public static final String[] months = {"12", "11", "10", "09", "08", "07", "06", "05", "04", "03", "02", "01"};
    public static final String[] years = {"2022", "2021", "2020","2019","2018", "2017"};

    @Override
    protected BaseDao<FinanceIncome, String> getDao() {
        return dao;
    }

    public List<FinanceIncome> queryList() {
        return dao.queryList();
    }

    public FinanceIncome queryByName(String name, String year, String month) {
        return dao.queryByName(name, year, month);
    }

    public List<FinanceIncome> queryListByDate(String year, String month) {
        return dao.queryListByDate(year, month);
    }

    public List<FinanceIncome> queryListByParentId(String parentId) {
        return dao.queryListByParentId(parentId);
    }

    public List<Map<String, Object>> queryFinancialIncomeComplete() {
        List<Map<String, Object>> FinancialIncomeComplete = new ArrayList<>();
        List<FinanceIncome> FinanceIncomelist = new ArrayList<>();
        for (int j = 0; j < FinanceIncomeService.years.length; j++) {
            for (int i = 0; i < FinanceIncomeService.months.length; i++) {
                FinanceIncomelist = dao.queryListByDate(years[j], months[i]);
                if (ObjectUtils.isNotBlank(FinanceIncomelist)) {
                    break;
                }
            }
        }
        for (FinanceIncome fil : FinanceIncomelist) {
            if ("税收收入小计".equals(fil.getName()) || "非税收入".equals(fil.getName())) {
                DecimalFormat df = new DecimalFormat("#.##");
                Map<String, Object> map3 = new HashMap<String, Object>();
                map3.put("currentYear", fil.getCurrentYearIncome());
                map3.put("lastYear", fil.getCurrentLastYearIncome());
                map3.put("increDecre", fil.getIncreDecre());
                map3.put("name", fil.getName());
                map3.put("units", fil.getUnits());
                map3.put("sysId", fil.getSysId());
                //String totalRatio=df.format(Double.parseDouble(fil.getCurrentYearIncome())/Double.parseDouble(fil.getCurrentYearIncome())*100);
                //map3.put("totalRatio", totalRatio);
                List<Map<String, Object>> lists = new ArrayList<>();
                List<FinanceIncome> irlist = dao.queryListByParentId(fil.getSysId());
                for (FinanceIncome fi : irlist) {
                    Map<String, Object> map5 = new HashMap<String, Object>();
                    map5.put("name", fi.getName());
                    map5.put("monthIncome", fi.getCurrentMonthIncome());       //本月收入
                    map5.put("yearIncome", fi.getCurrentYearIncome());         //本年累计收入
                    map5.put("completeBudget", fi.getCompleteBudget());        //完成年初预算
                    map5.put("lastYearIncome", fi.getCurrentLastYearIncome());  //上年累计收入
                    map5.put("increDecre", fi.getIncreDecre());                 //同比增减
                    map5.put("units", fi.getUnits());
                    lists.add(map5);
                }
                map3.put("lists", lists);
                FinancialIncomeComplete.add(map3);
            }
        }
        return FinancialIncomeComplete;
    }

    public String getSecondMonth(String currentDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMM");
        Date date = format.parse(currentDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
        date = calendar.getTime();
        String accDate = format.format(date);
        return accDate;
    }

    public List<FinanceIncome> queryByDate() {
        String date = dao.queryNewDate();
        String year = date.substring(0,4);
        String month = date.substring(5,7);
        List<FinanceIncome> list = dao.queryListByDate(year,month);
        return list;
    }

    public String queryNewDate(){
        String date = dao.queryNewDate();
        return date;
    }
}
